package com.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.mapper.BrandMapper;
import com.mall.model.domain.PmsBrand;
import com.mall.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult list(Integer pageNum, Integer pageSize, String keyword) {
        //创建分页对象  构建分页参数
        Page brandPage = new Page(pageNum, pageSize);
        //构建分页查询条件   根据关键字模糊查询
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(keyword), PmsBrand::getName, keyword);
        Page<PmsBrand> selectPage = brandMapper.selectPage(brandPage, queryWrapper);
        //返回分页数据
        return new PageResult(pageNum, pageSize, (int) selectPage.getTotal(), selectPage.getRecords());
    }

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    public List<PmsBrand> listAll() {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        List<PmsBrand> brandList = brandMapper.selectList(queryWrapper);
        return brandList;
    }

    /**
     * 添加品牌
     *
     * @param brand
     */
    public void save(PmsBrand brand) {
        brandMapper.insert(brand);
    }

    /**
     * 删除品牌
     *
     * @param id
     */
    public void delete(Integer id) {
        brandMapper.deleteById(id);
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     */
    public void deleteAll(List<Integer> ids) {
        brandMapper.deleteBatchIds(ids);
    }

    /**
     * 更新品牌
     *
     * @param id
     * @param brand
     */
    public void update(Integer id, PmsBrand brand) {
        brand.setId(Long.valueOf(id.toString()));
        brandMapper.updateById(brand);
    }


    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    public PmsBrand queryBrandById(Integer id) {
        return brandMapper.selectById(id);
    }


    /**
     * 批量更新厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     */
    public void updateAllFactoryStatus(Integer factoryStatus, List<Integer> ids) {
        //遍历ids集合,拿到每一个品牌对象,然后在修改状态
        for (Integer id : ids) {
            PmsBrand brand = brandMapper.selectById(id);
            brand.setFactoryStatus(factoryStatus);
            //调用api修改状态
            brandMapper.updateById(brand);
        }
    }


    /**
     * 批量更新显示状态
     *
     * @param showStatus
     * @param ids
     * @return
     */
    public void updateAllShowStatus(Integer showStatus, List<Integer> ids) {
        //遍历ids集合,拿到每一个品牌对象,然后在修改状态
        for (Integer id : ids) {
            PmsBrand brand = brandMapper.selectById(id);
            brand.setShowStatus(showStatus);
            //调用api修改状态
            brandMapper.updateById(brand);
        }
    }
}
